package com.anrongtec.zcpt.view;

import java.lang.reflect.Method;

import com.anrongtec.zcpt.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class KeyboardView extends LinearLayout {
	private TextView keyboard_tv1, keyboard_tv2, keyboard_tv3, keyboard_tv4, keyboard_tv5, keyboard_tv6, keyboard_tv7,
			keyboard_tv8, keyboard_tv9, keyboard_tv0, keyboard_tvX,keyboard_tv_ok, keyboard_tv_title;
	private RelativeLayout keyboard_iv_delete;
	// 键盘作用的InputView
//	private InputView inputView;
	private EditText editText;

	private static final String KEY_1 = "1";
	private static final String KEY_2 = "2";
	private static final String KEY_3 = "3";
	private static final String KEY_4 = "4";
	private static final String KEY_5 = "5";
	private static final String KEY_6 = "6";
	private static final String KEY_7 = "7";
	private static final String KEY_8 = "8";
	private static final String KEY_9 = "9";
	private static final String KEY_0 = "0";
	private static final String KEY_X = "X";

	
	
//	public IdnoKeyboardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//		super(context, attrs, defStyleAttr, defStyleRes);
//		LayoutInflater.from(context).inflate(R.layout.keyboard_view, this);
//		initViews();
//		initEvents();
//	}

	public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		LayoutInflater.from(context).inflate(R.layout.keyboard_view, this);
		initViews();
		initEvents();
	}

	public KeyboardView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public KeyboardView(Context context) {
		this(context, null);
	}

	private void initViews() {
		keyboard_tv1 = (TextView) findViewById(R.id.keyboard_tv1);
		keyboard_tv2 = (TextView) findViewById(R.id.keyboard_tv2);
		keyboard_tv3 = (TextView) findViewById(R.id.keyboard_tv3);
		keyboard_tv4 = (TextView) findViewById(R.id.keyboard_tv4);
		keyboard_tv5 = (TextView) findViewById(R.id.keyboard_tv5);
		keyboard_tv6 = (TextView) findViewById(R.id.keyboard_tv6);
		keyboard_tv7 = (TextView) findViewById(R.id.keyboard_tv7);
		keyboard_tv8 = (TextView) findViewById(R.id.keyboard_tv8);
		keyboard_tv9 = (TextView) findViewById(R.id.keyboard_tv9);
		keyboard_tv0 = (TextView) findViewById(R.id.keyboard_tv0);
		keyboard_tvX = (TextView) findViewById(R.id.keyboard_tvx);
		keyboard_tv_title = (TextView) findViewById(R.id.keyboard_tv_title);
		keyboard_tv_ok = (TextView) findViewById(R.id.keyboard_tv_ok);
		keyboard_iv_delete = (RelativeLayout) findViewById(R.id.keyboard_iv_delete);
	}

	private void initEvents() {
		OnClickListener listener = new OnKeyClickLsitener();
		keyboard_tv1.setOnClickListener(listener);
		keyboard_tv2.setOnClickListener(listener);
		keyboard_tv3.setOnClickListener(listener);
		keyboard_tv4.setOnClickListener(listener);
		keyboard_tv5.setOnClickListener(listener);
		keyboard_tv6.setOnClickListener(listener);
		keyboard_tv7.setOnClickListener(listener);
		keyboard_tv8.setOnClickListener(listener);
		keyboard_tv9.setOnClickListener(listener);
		keyboard_tv0.setOnClickListener(listener);
		keyboard_tvX.setOnClickListener(listener);

		keyboard_tv_ok.setOnClickListener(listener);
		keyboard_iv_delete.setOnClickListener(listener);

		keyboard_iv_delete.setOnLongClickListener(new OnKeyLongClickListener());
	}

	/**
	 * 点击添加新的字符
	 * 
	 * @param value
	 */
	private void addText(String key_value) {
		int index = editText.getSelectionEnd();
		// 为文本框设置内容
		StringBuilder sb = new StringBuilder(editText.getText());
		sb.insert(index, key_value);
		editText.setText(sb.toString());
		editText.setSelection(index+1);
	}

	/**
	 * 删除光标前面的字符
	 */
	private void deleteText() {
		int index = editText.getSelectionEnd();
		if (TextUtils.isEmpty(editText.getText()) || index == -1)
			return;
		StringBuilder sb = new StringBuilder(editText.getText());
		if (index != 0) {
			sb.deleteCharAt(index - 1);
			editText.setText(sb.toString());
			editText.setSelection(index - 1);
		}
	}

	private void setEditTextOnTouchListener() {
		editText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				HideKeyboard(editText);
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					KeyboardView.this.setVisibility(View.VISIBLE);
				}
				return false;
			}
		});
	}

	// 隐藏虚拟键盘
	private void HideKeyboard(View v) {
		InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive()) {
			imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
		}
	}

	private void setEditOnFocusChangListener() {
		editText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					KeyboardView.this.setVisibility(View.VISIBLE);
				} else {
					KeyboardView.this.setVisibility(View.GONE);
				}
			}
		});
	}

	private class OnKeyLongClickListener implements View.OnLongClickListener {

		@Override
		public boolean onLongClick(View v) {
			if (v.getId() == R.id.keyboard_iv_delete) {
				editText.setText("");
			}
			return false;
		}
	}

	private class OnKeyClickLsitener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			String key_value = "";
			switch (v.getId()) {
			case R.id.keyboard_tv1:
				key_value = KEY_1;
				break;
			case R.id.keyboard_tv2:
				key_value = KEY_2;
				break;
			case R.id.keyboard_tv3:
				key_value = KEY_3;
				break;
			case R.id.keyboard_tv4:
				key_value = KEY_4;
				break;
			case R.id.keyboard_tv5:
				key_value = KEY_5;
				break;
			case R.id.keyboard_tv6:
				key_value = KEY_6;
				break;
			case R.id.keyboard_tv7:
				key_value = KEY_7;
				break;
			case R.id.keyboard_tv8:
				key_value = KEY_8;
				break;
			case R.id.keyboard_tv9:
				key_value = KEY_9;
				break;
			case R.id.keyboard_tv0:
				key_value = KEY_0;
				break;
			case R.id.keyboard_tvx:
				key_value = KEY_X;
				break;

			case R.id.keyboard_tv_ok:
				KeyboardView.this.setVisibility(View.GONE);
				return;
			case R.id.keyboard_iv_delete:
				deleteText();
				return;
			}
			addText(key_value);
		}
	}

	/**
	 * 设置是否为身份证输入法
	 * 
	 * @param isIdno
	 *            true代表身份证输入法
	 */
	public void setIsIdno(boolean isIdno) {
		keyboard_tvX.setEnabled(isIdno);
		keyboard_tvX.setText(isIdno?"X":"");
		
		keyboard_tv_title.setText(isIdno ? "身份证输入法":"警号输入法");
	}

	/**
	 * 设置当前键盘作用的inputView
	 * 
	 * @param inputView
	 */
	public void setInputView(final EditText editText, Activity activity) {
		this.editText = editText;
		if (android.os.Build.VERSION.SDK_INT <= 10) {// 4.0以下 danielinbiti
			editText.setInputType(InputType.TYPE_NULL);
		} else {
			activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
			try {
				Class<EditText> cls = EditText.class;
				Method setShowSoftInputOnFocus;
				setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
				setShowSoftInputOnFocus.setAccessible(true);
				setShowSoftInputOnFocus.invoke(editText, false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		setEditTextOnTouchListener();
		setEditOnFocusChangListener();
	}
}
